<template>
  <el-checkbox-group v-model="dicValue" style="float: left" @change="change">
    <el-checkbox v-for="(item, index) in dicList" :key="index" :label="item.id">
      <span v-if="item.name !== showInputText">{{ item.value }}</span>
      <el-input
        v-else
        v-model="item.value"
        :placeholder="$t('ModuleDevice.components.custom')"
        @change="change"
      ></el-input>
    </el-checkbox>
  </el-checkbox-group>
</template>

<script>
import {getDictList} from "@/ModuleSystem/api/System/SystemDict.js";
import { showInputText } from "../utils/const";
export default {
  name: "DicCheckBoxGroupList",
  components: {},
  props: {
    // 查询条件下是否需要全部
    // 筛选条件
    filterType: {
      type: String,
      default: "",
    },
    isAll: {
      type: Boolean,
      default: false,
    },
    // 使用id或者使用code绑定
    valueIsIdOrCode: {
      type: Boolean,
      default: true,
    },
    dictCode: {
      type: String,
      default: "",
    },
    value: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      showInputText: showInputText,
      dicList: [],
      dicValue: [],
    };
  },
  watch: {},
  mounted() {
    this.initData();
  },
  methods: {
    change() {
      var data = [];
      this.dicList.map((item) => {
        if (this.dicValue.indexOf(item.id) >= 0) {
          var obj = {};
          obj.id = item.id;
          obj.name = item.name;
          obj.value = item.value;
          data.push(JSON.parse(JSON.stringify(obj)));
        }
      });
      this.$emit("input", data);
    },
    initData() {
      this.dicValue = [];
      getDictList({ dictCode: this.dictCode })
        .then((res) => {
          this.dicList = res.data.reverse();
          if (this.value.length > 0) {
            this.value.map((item) => {
              this.dicValue.push(item.id);
            });
          }
          this.dicList.map((item) => {
            if (showInputText !== item.name) {
              item.value = item.name;
            } else {
              if (this.value.length > 0) {
                var objList = this.value.filter((item2) => {
                  return item2.id === item.id;
                });
                if (objList.length > 0) {
                  item.value = objList[0].value;
                } else {
                  item.value = "";
                }
              } else {
                item.value = "";
              }
            }
          });
        });
    },
  },
};
</script>
<style scoped></style>
